home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 655 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  5.3 KB

  1. Path: chronicle.mti.sgi.com!austern
  2. From: clamage@Eng.Sun.COM (Steve Clamage)
  3. Newsgroups: comp.std.c++
  4. Subject: Re: 'const' in header files
  5. Date: 07 Mar 1996 09:23:31 PST
  6. Organization: Sun Microsystems Inc.
  7. Approved: austern@isolde.mti.sgi.com
  8. Message-ID: <4hn54s$3am@engnews1.Eng.Sun.COM>
  9. References: <AD64B66E966816216@sleipner.nts.mh.se>
  10. Reply-To: clamage@Eng.Sun.COM
  11. NNTP-Posting-Host: isolde.mti.sgi.com
  12. X-Original-Date: 7 Mar 1996 17:07:40 GMT
  13. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  14.     iQBVAwUBMT8bqEy4NqrwXLNJAQHCwQIAt5yoaDXz+qcHM13Zs9NRYNbEDb/4LqPj
  15.     3qXfIu2RIRdHdVsq5BnyG79WkVKNaD5CtCnLfiqEmCBI4cbDZ8C0GQ==
  16.     =CLX8
  17. Originator: austern@isolde.mti.sgi.com
  18.  
  19. In article AD64B66E966816216@sleipner.nts.mh.se, lars.farm@nts.mh.se (Lars Farm) writes:
  20. >
  21. >... People rely on the fact that
  22. >unused constants are optimized away. Unfortunately, it appears that some
  23. >compiler writers do this only for the special case of integer types. I see
  24. >no reason why this should be any different:
  25. >
  26. >  const float F = 1.234;     //should be preferable to "#define F 1.234"
  27. >
  28. >Isuggested that required behaviour was that these constants are not
  29. >instantiated unless actually used, much like templates. Rather than leaving
  30. >it as an optional optimization as it appears to be now. This should apply
  31. >to any built in type. Thus invalidating stupid warnings about unused
  32. >variables. If some compilers warn about this, and according to the thread
  33. >in c.l.c++.moderated some do, then many users will have to revert to the
  34. >#define style constants instead. Is that the intent of the language
  35. >designers?
  36.  
  37. I tried to answer this once already. I'll try again.
  38.  
  39. There is a limit to what you can or should try to specify in the language
  40. definition.
  41.  
  42. For one thing, warnings are not mentioned as such at all. A compiler can
  43. warn about anything it likes. ("Warning: function A contains more than
  44. 25 lines of code." "Warning: function B contains fewer than 25 lines
  45. of code.") Whether you think a particular warning is a good or a bad
  46. idea, and whether you can or want to turn off all messages or just a
  47. particular warning is up to you, the compiler vendor, and whoever
  48. writes the programming standards in your shop. The language definition is
  49. silent on this issue, and quite deliberately so.
  50.  
  51. For another thing, language requirements have to be testable for them to
  52. make sense. If you try to make a requirement that certain constructs be
  53. optimized away when they can be optimized away, how do you test for
  54. compliance? Any test you make to see whether such a thing is present
  55. must report that it is present, or the compiler violates the language rules.
  56. The optimization can occur only if you don't try to find out whether it
  57. occurred. (There are exceptions, but const objects are not one of the
  58. exceptions).
  59.  
  60. Next, why single out this one particular optimization? Why not concentrate
  61. on optimizations that have a significant impact on program performance,
  62. like creation and destruction of extra temporaries. The effect of of an
  63. extra floating-point object in the program is negligible compared to the
  64. proliferation of temporary objects that are not actually required.
  65.  
  66. The whole area of optimization and unneeded code and data is deliberately
  67. not part of the language definition. Those things are left as a "quality of
  68. implementation" issue. Different programs and programmers have different
  69. criteria for what is important, and IMHO C++ implementors should not be
  70. prohibited from satisfying user requirements.
  71.  
  72. If you require a space optimization, it might make the code run slower. If
  73. you require a time optimization, it might make the program larger. If you
  74. require a set of optimizations that can only make the program both smaller
  75. and faster, it might mean the compiler gets bigger and slower to the point
  76. where it isn't useful. These are not just theoretical points. Some compilers
  77. have captured a large market segment by compiling very quickly, even though
  78. the resulting programs were not very efficient. Many users cared more
  79. about the compile/test/rewrite cycle than a percentage difference in
  80. program performance. Would you deny certification to a compiler that
  81. generates correct code and performs acceptably for its customers even if it
  82. does not make any optimizations? I don't insist that you buy this compiler;
  83. I do insist that I be allowed to buy it.
  84.  
  85. I suppose I should also comment on the comparison with unneeded template
  86. instantiations. That isn't an optimization issue, but a correctness issue.
  87. Simple example:
  88.     template< class T > class X {
  89.         T t;
  90.         ...
  91.         bool operator<( const T& rhs ) { return t < rhs.t; }
  92.     };
  93. Suppose we instantiate X on a type T for which the "<" operator is not
  94. defined. That isn't a problem unless we invoke X<T>::operator<. So the
  95. language rule is that a compiler can't reject a program due to an
  96. invalid instantiation unless the instantiation is actually required. We
  97. can test compiler compliance by instantiating X on various T's which
  98. do and do not have a "<" operator and observe the results when we do
  99. and do not try to use the "<" operator.
  100. ---
  101. Steve Clamage, stephen.clamage@eng.sun.com
  102. ---
  103. [ comp.std.c++ is moderated.  To submit articles: Try just posting with your 
  104.                 newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  105.   comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  106.   Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  107.   Comments? mailto:std-c++-request@ncar.ucar.edu 
  108. ]
  109.